import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont


width = 600
height = 500
imageshape =(650,910,3)


offsetx = int((910 - width)/2)

FrameLassImage = 0
FrameImage = 16


image = np.zeros(imageshape, dtype=np.uint8)
pointlist = []
pointinfolist = []

image.fill(255)

cv2.rectangle(image,(offsetx-5,75-5),(offsetx+width+5,75+height+5),(0,0,0),10)


food =      ["水稻","玉米","花生","蚕豆","番薯"]
fruits =    ["橙子","苹果","榴莲","葡萄","香蕉"]
vegetables =["白菜","黄瓜","辣椒","萝卜","茄子"]

def cv2ImgAddText(img, text, left, top, textColor=(255, 0, 0), textSize=20):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)


while len(pointlist) < FrameImage:
    x = np.random.randint(0,width/20)*20
    y = np.random.randint(0,height/20)*20
    if (x,y) not in pointlist:
        pointlist.append((x,y))
    else:
        continue
    tmp = np.random.randint(0,15)      
    pointinfolist.append(tmp)


randomlist = []
randomlistsInfo = []
while len(randomlist) < FrameLassImage:
    x = np.random.randint(0,width/20)*20
    y = np.random.randint(0,height/20)*20
    if (x,y) not in randomlist and (x,y) not in pointlist:
        randomlist.append((x,y))
    else:
        continue
    tmp = np.random.randint(0,15)
    randomlistsInfo.append(tmp)


        
image2 = image.copy()

for it in range(len(pointlist)):
    tmpx,tmpy = pointlist[it]
    cv2.circle(image,(tmpx+offsetx+10,tmpy+75+10),0,(0,0,0),18)
    cv2.circle(image2,(tmpx+offsetx+10,tmpy+75+10),0,(0,0,0),18)

   
for it in range(len(randomlist)): 
    tmpx,tmpy = randomlist[it]
    cv2.circle(image2,(tmpx+offsetx+10,tmpy+75+10),0,(0,255,0),18)


for it in range(len(pointlist)): 
    tmp2=  pointinfolist[it]
    tmpx,tmpy = pointlist[it]
    if tmp2 < 5:
        image2 = cv2ImgAddText(image2,food[tmp2]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)
    elif tmp2 <10:
        image2 = cv2ImgAddText(image2,fruits[tmp2-5]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)
    else:  
        image2 = cv2ImgAddText(image2,vegetables[tmp2-10]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)

for it in range(len(randomlist)): 
    tmp2=  randomlistsInfo[it]
    tmpx,tmpy = randomlist[it]
    if tmp2 < 5:
        image2 = cv2ImgAddText(image2,food[tmp2]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)
    elif tmp2 <10:
        image2 = cv2ImgAddText(image2,fruits[tmp2-5]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)
    else:  
        image2 = cv2ImgAddText(image2,vegetables[tmp2-10]+"(%d,%d)"%(tmpx/20,24-tmpy/20),tmpx+100,tmpy+90)



cv2.imshow("image", image)
cv2.imshow("image2", image2)
cv2.imwrite("CoodMap\\point.jpg", image)
cv2.imwrite("CoodMap\\point2.jpg", image2)
cv2.waitKey(0)

